/**
 * 15/02/2014
 * 
 * StateController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.StateBOInterface;
import org.cuatrovientos.erp.model.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller class for State management
 * @author Javier Tejedor
 */
@Controller
public class StateController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(StateController.class);

	@Autowired
	private StateBOInterface stateBO;

	/**
	 * default constructor
	 */
	public StateController () {
		logger.info("State BEAN instantiated");
	}

	/**
	 * handles default /state or /state/list request
	 * @return
	 */
	@RequestMapping({"/state","/state/list"})
	public ModelAndView stateDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<State> stateList = stateBO.selectAll();
		model.put("states", stateList );
		
		return new ModelAndView("StateList", model);     
	}


	/**
	 * handles default /state/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/state/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<State> stateList = stateBO.search(description);
		model.put("states", stateList );
		
		return new ModelAndView("StateList", model);     
	}

	/**
	 * handles default /state/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/state/delete"})
	public String deleteState(@RequestParam(value="id", defaultValue="",
	required=true) int id) {
		
		logger.info("delete " + id);

		stateBO.deleteById(id);
		
		return "redirect:/state";
		
	}

	/**
	 * handles default /state/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/state/new"})
	public ModelAndView newState() {
		logger.info("new state");
		
		State state = new State();

		return new ModelAndView("NewState", "state", state);
	}

	/**
	 * handles default /state/save request
	 * @param state
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/state/save"})
	public String newState(@Valid State state, BindingResult bindingResult) {
		logger.info("new state " + state.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("state", state);
			
			return "NewState";
		}

		stateBO.insert(state);
		
		return "redirect:/state";
	}

	/**
	 * handles default /state/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/state/detail"})
	public ModelAndView stateDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		State state = stateBO.selectById(id);
		model.put("state", state);

		return new ModelAndView("StateDetail", model);
	}


	/**
	 * handles default /state/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/state/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		State state = stateBO.selectById(id);
		model.put("state", state);
		
		return new ModelAndView("UpdateState",model);
	}


	/**
	 * handles default /state/saveupdate request
	 * @param state
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/state/saveupdate"})
	public String saveUpdate(@Valid State state, BindingResult bindingResult) {
		logger.info("update state " + state.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("state", state);
			
			return "UpdateState";
		}

		stateBO.update(state);
		
		return "redirect:/state";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/state/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
